package at.guthy;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class PicasaClient {
	private static PicasaClient instance = null;
	private PicasawebService myService;
	private static String username = "";

	private PicasaClient() throws AuthenticationException {
		myService = new PicasawebService("exampleCo-exampleApp-1");
	};

	public void login() throws AuthenticationException {
		myService.setUserCredentials(PicasaWindow.usernameField.getText(), PicasaWindow.passwordField.getText());
		username = PicasaWindow.usernameField.getText().replace("@google.com", "");
	}

	public static PicasaClient getInstance() throws AuthenticationException {
		if (instance == null) {
			instance = new PicasaClient();
		}
		return instance;
	}

	public ArrayList<AlbumEntry> getAlbums() {
		ArrayList<AlbumEntry> ret = new ArrayList<AlbumEntry>();
		if (username == null || username.isEmpty()) { return ret; }
		try {

			URL feedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/" + username + "?kind=album");

			UserFeed myUserFeed = myService.getFeed(feedUrl, UserFeed.class);

			for (AlbumEntry myAlbum : myUserFeed.getAlbumEntries()) {
				ret.add(myAlbum);
			}
		}
		catch (MalformedURLException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ServiceException e) {
			e.printStackTrace();
		}

		return ret;
	}

	public ArrayList<PhotoEntry> getPhotos(String albumid) {
		ArrayList<PhotoEntry> ret = new ArrayList<PhotoEntry>();
		if (username == null || username.isEmpty()) { return ret; }

		try {
			URL feedUrl = new URL(albumid);

			AlbumFeed myAlbumFeed = myService.getFeed(feedUrl, AlbumFeed.class);
			System.out.println(myAlbumFeed.getPhotoEntries().toArray().length);
			for (PhotoEntry myPhoto : myAlbumFeed.getPhotoEntries()) {
				ret.add(myPhoto);
			}

		}
		catch (AuthenticationException e) {
			e.printStackTrace();
		}
		catch (MalformedURLException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ServiceException e) {
			e.printStackTrace();
		}

		return ret;
	}

	public void uploadPhotos(File[] files, AlbumEntry album) {
		if (username.isEmpty()) { return; }
		for (File file : files)
			try {
				System.out.println("Upload " + file.getName());
				URL albumPostUrl = new URL("http://picasaweb.google.com/data/feed/api/user/" + username + "/albumid/" + album.getGphotoId());

				PhotoEntry myPhoto = new PhotoEntry();
				myPhoto.setTitle(new PlainTextConstruct(file.getName()));
				myPhoto.setDescription(new PlainTextConstruct(file.getName()));
				myPhoto.setClient("PGJavaUploader");

				MediaFileSource myMedia = new MediaFileSource(file, "image/jpeg");
				myPhoto.setMediaSource(myMedia);
				myService.insert(albumPostUrl, myPhoto);
				System.out.println("Done");
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			catch (ServiceException e) {
				e.printStackTrace();
			}
	}
}
